﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms.Integration;
using System.Windows.Media;

namespace EnvControlLib
{
    /// <summary>
    /// 视频窗口
    /// </summary>
    public class ViewModel_EnvVideoPanel : NotificationObject
    {
        #region Private Field

        private DelegateCommand<string> _cmdMenuItemClick, _cmdToolbarClick;

        #endregion

        #region Constructor

        /// <summary>
        /// 基构造函数
        /// </summary>
        private ViewModel_EnvVideoPanel()
        {

        }

        /// <summary>
        /// （请设置属性）
        /// </summary>
        /// <param name="currentPanel">获取当前 Panel</param>
        public ViewModel_EnvVideoPanel(System.Windows.Forms.Panel currentPanel)
            : this()
        {
            if (currentPanel == null)
                throw new Exception("请设置 System.Windows.Forms.Panel");

            _currentPanel = currentPanel;
        }

        /// <summary>
        /// （请设置 Action 属性）
        /// </summary>
        /// <param name="isMaxPanel">是否是最大化窗口</param>
        /// <param name="panelCurrentList">主界面中的所有视频窗口</param>
        /// <param name="splitScreen">主界面中的视频窗口容器</param>
        /// <param name="maxScreen">最大化视频窗口</param>
        public ViewModel_EnvVideoPanel(bool isMaxPanel,
            List<EnvVideoPanel> panelCurrentList,
            System.Windows.Controls.Panel splitScreen,
            EnvVideoPanel maxScreen)
            : this()
        {
            this._isMaxPanel = isMaxPanel;
            this._panelCurrentList = panelCurrentList;
            this._splitScreen = splitScreen;
            this._maxScreen = maxScreen;
        }

        /// <summary>
        /// 包含所有 Action 属性
        /// </summary>
        /// <param name="isMaxPanel">是否是最大化窗口</param>
        /// <param name="panelCurrentList">主界面中的所有视频窗口</param>
        /// <param name="splitScreen">主界面中的视频窗口容器</param>
        /// <param name="maxScreen">最大化视频窗口</param>
        /// <param name="doubleAction">共同的双击事件</param>
        /// <param name="changeBorderColorAction">修改边框颜色</param>
        /// <param name="showMaxScreenAction">最大化视频窗口</param>
        /// <param name="hideMaxScreenAction">最小化视频窗口</param>
        /// <param name="stopPanelRealPlayAction">停止指定窗口中的视频</param>
        /// <param name="recordVideoAction">流数据录像</param>
        /// <param name="playBackAction">远程录像回放</param>
        /// <param name="screenShotAction">视频窗口截图</param>
        public ViewModel_EnvVideoPanel(bool isMaxPanel,
            List<EnvVideoPanel> panelCurrentList,
            System.Windows.Controls.Panel splitScreen,
            EnvVideoPanel maxScreen,
            Action doubleAction = null,
            Action<EnvVideoPanel, List<EnvVideoPanel>> changeBorderColorAction = null,
            Action<EnvVideoPanel, System.Windows.Controls.Panel, EnvVideoPanel, Action> showMaxScreenAction = null,
            Action<System.Windows.Controls.Panel, EnvVideoPanel, Action> hideMaxScreenAction = null,
            Action<EnvVideoPanel> stopPanelRealPlayAction = null,
            Action<EnvVideoPanel> recordVideoAction = null,
            Action<EnvVideoPanel> playBackAction = null,
            Action<EnvVideoPanel, string> screenShotAction = null)
            : this(isMaxPanel, panelCurrentList, splitScreen, maxScreen)
        {
            this._doubleAction = doubleAction;
            this._changeBorderColorAction = changeBorderColorAction;
            this._showMaxScreenAction = showMaxScreenAction;
            this._hideMaxScreenAction = hideMaxScreenAction;
            this._stopPanelRealPlayAction = stopPanelRealPlayAction;
            this._recordVideoAction = recordVideoAction;
            this._playBackAction = playBackAction;
            this._screenShotAction = screenShotAction;
        }

        #endregion

        #region Public Property

        /// <summary>
        /// 右键菜单选择
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand<string> Cmd_MenuItemClick
        {
            get
            {
                if (_cmdMenuItemClick == null)
                {
                    _cmdMenuItemClick = new DelegateCommand<string>(_menuItemClick, _canMenuItemClick);
                }
                return _cmdMenuItemClick;
            }
        }

        /// <summary>
        /// 工具栏选择
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand<string> Cmd_ToolbarClick
        {
            get
            {
                if (_cmdToolbarClick == null)
                {
                    _cmdToolbarClick = new DelegateCommand<string>(_toolbarClick, _canToolbarClick);
                }
                return _cmdToolbarClick;
            }
        }


        private bool _isMaxPanel;
        /// <summary>
        /// 是否是最大化视频窗口
        /// </summary>
        public bool IsMaxPanel
        {
            get
            {
                return _isMaxPanel;
            }
            set
            {
                if (_isMaxPanel == value) return;
                _isMaxPanel = value;
                base.RaisePropertyChanged("IsMaxPanel");
            }
        }

        private List<EnvVideoPanel> _panelCurrentList;
        /// <summary>
        /// 主界面中的所有视频窗口
        /// </summary>
        public List<EnvVideoPanel> PanelCurrentList
        {
            get
            {
                return _panelCurrentList;
            }
            set
            {
                if (_panelCurrentList == value) return;
                _panelCurrentList = value;
                base.RaisePropertyChanged("PanelCurrentList");
            }
        }

        private System.Windows.Controls.Panel _splitScreen;
        /// <summary>
        /// 主界面中的视频窗口容器
        /// </summary>
        public System.Windows.Controls.Panel SplitScreen
        {
            get
            {
                return _splitScreen;
            }
            set
            {
                if (_splitScreen == value) return;
                _splitScreen = value;
                base.RaisePropertyChanged("SplitScreen");
            }
        }

        private EnvVideoPanel _maxScreen;
        /// <summary>
        /// 最大化视频窗口
        /// </summary>
        public EnvVideoPanel MaxScreen
        {
            get
            {
                return _maxScreen;
            }
            set
            {
                if (_maxScreen == value) return;
                _maxScreen = value;
                base.RaisePropertyChanged("MaxScreen");
            }
        }

        private EnvVideoPanel _videoPanelControl;
        /// <summary>
        /// 当前视频窗口
        /// </summary>
        public EnvVideoPanel VideoPanelControl
        {
            get
            {
                return _videoPanelControl;
            }
            set
            {
                if (_videoPanelControl == value) return;
                _videoPanelControl = value;
                base.RaisePropertyChanged("VideoPanelControl");
            }
        }

        private System.Windows.Forms.Panel _currentPanel;
        /// <summary>
        /// 获取当前 Panel
        /// </summary>
        public System.Windows.Forms.Panel CurrentPanel
        {
            get
            {
                return _currentPanel;
            }
        }

        public string _videoInformation;
        /// <summary>
        /// 当前视频文字信息
        /// </summary>
        public string VideoInformation
        {
            get
            {
                return _videoInformation;
            }
            set
            {
                if (_videoInformation == value) return;
                _videoInformation = value;
                base.RaisePropertyChanged("VideoInformation");
            }
        }

        /// <summary>
        /// 记录临时信息，防止最大化关闭后再分屏未出现标题栏
        /// </summary>
        public string TempInformation { get; set; }

        public Brush _borderColor;
        /// <summary>
        /// 边框颜色
        /// </summary>
        public Brush BorderColor
        {
            get
            {
                return _borderColor;
            }
            set
            {
                if (_borderColor == value) return;
                _borderColor = value;
                base.RaisePropertyChanged("BorderColor");
            }
        }

        private Visibility _isRealPlayPanel;
        /// <summary>
        /// 是否是实时播放视频窗口
        /// </summary>
        public Visibility IsRealPlayPanel
        {
            get
            {
                return _isRealPlayPanel;
            }
            set
            {
                if (_isRealPlayPanel == value) return;
                _isRealPlayPanel = value;
                base.RaisePropertyChanged("IsRealPlayPanel");
            }
        }



        private Action _doubleAction;
        /// <summary>
        /// 共同的双击事件
        /// </summary>
        public Action DoubleAction
        {
            get
            {
                return _doubleAction;
            }
            set
            {
                if (_doubleAction == value) return;
                _doubleAction = value;
                base.RaisePropertyChanged("DoubleAction");
            }
        }

        private Action<EnvVideoPanel, List<EnvVideoPanel>> _changeBorderColorAction;
        /// <summary>
        /// 修改边框颜色
        /// </summary>
        public Action<EnvVideoPanel, List<EnvVideoPanel>> ChangeBorderColorAction
        {
            get
            {
                return _changeBorderColorAction;
            }
            set
            {
                if (_changeBorderColorAction == value) return;
                _changeBorderColorAction = value;
                base.RaisePropertyChanged("ChangeBorderColorAction");
            }
        }

        private Action<EnvVideoPanel, System.Windows.Controls.Panel, EnvVideoPanel, Action> _showMaxScreenAction;
        /// <summary>
        /// 最大化视频窗口
        /// </summary>
        public Action<EnvVideoPanel, System.Windows.Controls.Panel, EnvVideoPanel, Action> ShowMaxScreenAction
        {
            get
            {
                return _showMaxScreenAction;
            }
            set
            {
                if (_showMaxScreenAction == value) return;
                _showMaxScreenAction = value;
                base.RaisePropertyChanged("ShowMaxScreenAction");
            }
        }

        private Action<System.Windows.Controls.Panel, EnvVideoPanel, Action> _hideMaxScreenAction;
        /// <summary>
        /// 最小化视频窗口
        /// </summary>
        public Action<System.Windows.Controls.Panel, EnvVideoPanel, Action> HideMaxScreenAction
        {
            get
            {
                return _hideMaxScreenAction;
            }
            set
            {
                if (_hideMaxScreenAction == value) return;
                _hideMaxScreenAction = value;
                base.RaisePropertyChanged("HideMaxScreenAction");
            }
        }

        private Action<EnvVideoPanel> _stopPanelRealPlayAction;
        /// <summary>
        /// 停止指定窗口中的视频
        /// </summary>
        public Action<EnvVideoPanel> StopPanelRealPlayAction
        {
            get
            {
                return _stopPanelRealPlayAction;
            }
            set
            {
                if (_stopPanelRealPlayAction == value) return;
                _stopPanelRealPlayAction = value;
                base.RaisePropertyChanged("StopPanelRealPlayAction");
            }
        }

        private Action<EnvVideoPanel> _recordVideoAction;
        /// <summary>
        /// 流数据录像
        /// </summary>
        public Action<EnvVideoPanel> RecordVideoAction
        {
            get
            {
                return _recordVideoAction;
            }
            set
            {
                if (_recordVideoAction == value) return;
                _recordVideoAction = value;
                base.RaisePropertyChanged("RecordVideoAction");
            }
        }

        private Action<EnvVideoPanel> _playBackAction;
        /// <summary>
        /// 远程录像回放
        /// </summary>
        public Action<EnvVideoPanel> PlayBackAction
        {
            get
            {
                return _playBackAction;
            }
            set
            {
                if (_playBackAction == value) return;
                _playBackAction = value;
                base.RaisePropertyChanged("PlayBackAction");
            }
        }

        private Action<EnvVideoPanel, string> _screenShotAction;
        /// <summary>
        /// 视频窗口截图
        /// </summary>
        public Action<EnvVideoPanel, string> ScreenShotAction
        {
            get
            {
                return _screenShotAction;
            }
            set
            {
                if (_screenShotAction == value) return;
                _screenShotAction = value;
                base.RaisePropertyChanged("ScreenShotAction");
            }
        }

        #endregion

        #region Event

        /// <summary>
        /// 1. 先初始化加载 EnvVideoPanel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void videoPanelControl_Loaded(object sender, RoutedEventArgs e)
        {
            _videoPanelControl = sender as EnvVideoPanel;
        }

        /// <summary>
        /// 2. 再初始化加载 WindowsFormsHost
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void formHost_Loaded(object sender, RoutedEventArgs e)
        {
            WindowsFormsHost _formHost = sender as WindowsFormsHost;
            if (_formHost.Tag != null &&
                _formHost.Tag is System.Windows.Forms.Panel)
            {
                _currentPanel = _formHost.Tag as System.Windows.Forms.Panel; // 得到 Panel
            }

            if (_videoPanelControl != null)
            {
                // 修改视频窗口宽度与高度
                _formHost.Width = _videoPanelControl.Width - 2;
                _formHost.Height = _videoPanelControl.Height - 25;
            }
        }

        #region 单击事件

        /// <summary>
        /// 单击视频窗口时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void videoPanelControl_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            videoPanelControl_MouseDown(sender, null); // 修改边框颜色
        }

        /// <summary>
        /// 单击标题栏时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void videoPanelControl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!this._isMaxPanel &&
                this._changeBorderColorAction != null) // 只针对一般视频窗口
            {
                this._changeBorderColorAction(sender as EnvVideoPanel,
                    this._panelCurrentList); // 修改边框颜色
            }
        }

        #endregion

        #region 双击事件

        /// <summary>
        /// 双击视频窗口事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void videoPanelControl_MouseDoubleClick(object sender, 
            System.Windows.Input.MouseButtonEventArgs e)
        {
            #region 最大化视频窗口操作
            if (this._maxScreen != null && this._isMaxPanel &&
                this._maxScreen.Visibility == System.Windows.Visibility.Visible &&
                this._hideMaxScreenAction != null) // 双击标题栏时最小化
            {
                this._hideMaxScreenAction(this._splitScreen, this._maxScreen,
                    this._doubleAction); // 最小化视频窗口
            }
            #endregion

            #region 一般视频窗口操作
            if (this._maxScreen != null && !this._isMaxPanel &&
                this._maxScreen.Visibility == System.Windows.Visibility.Collapsed &&
                this._showMaxScreenAction != null) // 双击标题栏时最大化
            {
                this._showMaxScreenAction(sender as EnvVideoPanel,
                    this._splitScreen, this._maxScreen, this._doubleAction); // 最大化视频窗口
            }
            #endregion
        }

        #endregion

        #endregion

        #region Private Method

        #region DelegateCommand

        private bool _canMenuItemClick(string menu)
        {
            return true;
        }

        private bool _canToolbarClick(string toolbar)
        {
            return true;
        }


        /// <summary>
        /// 右键菜单选择
        /// </summary>
        /// <param name="menu"></param>
        private void _menuItemClick(string menu)
        {
            switch (menu)
            {
                case "closeCurrentPanel": // 关闭当前视频
                    _closeCurrentPanel();
                    break;
                case "closeAllPanel": // 关闭所有视频
                    _closeAllPanel();
                    break;
                case "panelMode": // 更改窗口模式（最大化，还原）
                    videoPanelControl_MouseDoubleClick(_videoPanelControl, null);
                    break;
                case "recordVideo": // 视频流录像
                    _recordVideo();
                    break;
                case "screenShot": // 视频窗口截图
                    _screenShot();
                    break;
                case "playBack": // 远程录像回放
                    _playBack();
                    break;



                default:
                    break;
            }
        }

        /// <summary>
        /// 工具栏选择
        /// </summary>
        /// <param name="toolbar"></param>
        private void _toolbarClick(string toolbar)
        {
            switch (toolbar)
            {
                case "RecordVideo": // 视频流录像
                    _recordVideo();
                    break;
                case "ScreenShot": // 视频窗口截图
                    _screenShot();
                    break;
                case "PanelMode": // 更改窗口模式（最大化，还原）
                    videoPanelControl_MouseDoubleClick(_videoPanelControl, null);
                    break;
                case "ClosePanel": // 关闭当前视频窗口
                    _closeCurrentPanel();
                    break;



                default:
                    break;
            }
        }

        #endregion

        /// <summary>
        /// 关闭当前视频窗口
        /// </summary>
        private void _closeCurrentPanel()
        {
            if (this._stopPanelRealPlayAction != null)
                this._stopPanelRealPlayAction(_videoPanelControl); // 停止当前视频

            if (this._isMaxPanel && _maxScreen != null) // 为最大化视频窗口时
                videoPanelControl_MouseDoubleClick(_maxScreen, null); // 隐藏最大化视频窗口
        }

        /// <summary>
        /// 关闭所有视频窗口
        /// </summary>
        private void _closeAllPanel()
        {
            if (this._stopPanelRealPlayAction != null &&
                _panelCurrentList != null && _panelCurrentList.Count > 0)
            {
                foreach (EnvVideoPanel singlePanel in _panelCurrentList)
                {
                    this._stopPanelRealPlayAction(singlePanel); // 停止当前视频
                }
            }
        }

        /// <summary>
        /// 视频流录像
        /// </summary>
        private void _recordVideo()
        {
            if (this._recordVideoAction != null)
                this._recordVideoAction(_videoPanelControl); // 显示视频流录像界面
        }
        
        /// <summary>
        /// 远程录像回放
        /// </summary>
        private void _playBack()
        {
            if (this._playBackAction != null)
                this._playBackAction(_videoPanelControl); // 显示远程录像回放界面
        }

        /// <summary>
        /// 视频窗口截图
        /// </summary>
        private void _screenShot()
        {
            if (this._screenShotAction != null)
                this._screenShotAction(_videoPanelControl, null); // 当前视频窗口截图操作
        }

        #endregion

        #region Public Method

        /// <summary>
        /// 右键菜单功能项
        /// </summary>
        /// <param name="menu">MenuItem Name</param>
        public void CallMenuItem(string menu)
        {
            _menuItemClick(menu);
        }

        #endregion
    }
}
